package com.gqj.elec.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.gqj.common.utils.DateUtils;
import com.gqj.common.utils.uuid.HiultraUtil;
import com.gqj.elec.domain.ElecMaterial;
import com.gqj.elec.domain.ElecWarning;
import com.gqj.elec.domain.ElecWarningDetail;
import com.gqj.elec.mapper.ElecMaterialMapper;
import com.gqj.elec.mapper.ElecWarningDetailMapper;
import com.gqj.elec.mapper.ElecWarningMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.gqj.elec.mapper.ElecMaterialRepairdetailMapper;
import com.gqj.elec.domain.ElecMaterialRepairdetail;
import com.gqj.elec.service.IElecMaterialRepairdetailService;

/**
 * 物资检修详情Service业务层处理
 *
 * @author lxq
 * @date 2023-12-17
 */
@Service
@Component("ElecMaterialRepairdetailServiceImpl")
public class ElecMaterialRepairdetailServiceImpl implements IElecMaterialRepairdetailService {
    @Autowired
    private ElecMaterialRepairdetailMapper elecMaterialRepairdetailMapper;
    @Autowired
    private ElecMaterialMapper elecMaterialMapper;
    @Autowired
    private ElecWarningMapper elecWarningMapper;
    @Autowired
    private ElecWarningDetailMapper elecWarningDetailMapper;

    /**
     * 查询物资检修详情
     *
     * @param repairdetailId 物资检修详情主键
     * @return 物资检修详情
     */
    @Override
    public ElecMaterialRepairdetail selectElecMaterialRepairdetailByRepairdetailId(Long repairdetailId) {
        return elecMaterialRepairdetailMapper.selectElecMaterialRepairdetailByRepairdetailId(repairdetailId);
    }

    /**
     * 查询物资检修详情列表
     *
     * @param elecMaterialRepairdetail 物资检修详情
     * @return 物资检修详情
     */
    @Override
    public List<ElecMaterialRepairdetail> selectElecMaterialRepairdetailList(ElecMaterialRepairdetail elecMaterialRepairdetail) {
        return elecMaterialRepairdetailMapper.selectElecMaterialRepairdetailList(elecMaterialRepairdetail);
    }

    /**
     * 新增物资检修详情
     *
     * @param elecMaterialRepairdetail 物资检修详情
     * @return 结果
     */
    @Override
    public int insertElecMaterialRepairdetail(ElecMaterialRepairdetail elecMaterialRepairdetail) {
        elecMaterialRepairdetail.setCreateTime(DateUtils.getNowDate());
        return elecMaterialRepairdetailMapper.insertElecMaterialRepairdetail(elecMaterialRepairdetail);
    }

    /**
     * 修改物资检修详情
     *
     * @param elecMaterialRepairdetail 物资检修详情
     * @return 结果
     */
    @Override
    public int updateElecMaterialRepairdetail(ElecMaterialRepairdetail elecMaterialRepairdetail) {
        elecMaterialRepairdetail.setUpdateTime(DateUtils.getNowDate());
        return elecMaterialRepairdetailMapper.updateElecMaterialRepairdetail(elecMaterialRepairdetail);
    }

    /**
     * 批量删除物资检修详情
     *
     * @param repairdetailIds 需要删除的物资检修详情主键
     * @return 结果
     */
    @Override
    public int deleteElecMaterialRepairdetailByRepairdetailIds(Long[] repairdetailIds) {
        return elecMaterialRepairdetailMapper.deleteElecMaterialRepairdetailByRepairdetailIds(repairdetailIds);
    }

    /**
     * 删除物资检修详情信息
     *
     * @param repairdetailId 物资检修详情主键
     * @return 结果
     */
    @Override
    public int deleteElecMaterialRepairdetailByRepairdetailId(Long repairdetailId) {
        return elecMaterialRepairdetailMapper.deleteElecMaterialRepairdetailByRepairdetailId(repairdetailId);
    }

    /**
     * 检修过期查询
     *
     * @return 结果
     */
    public int repairOverdueQuery() {
        List<ElecMaterialRepairdetail> elecMaterialRepairdetail = elecMaterialRepairdetailMapper.selectRepairOverdue();
        Long warningId = null;
        for (ElecMaterialRepairdetail entity : elecMaterialRepairdetail) {
            Date nowDate = new Date();
            Date repairDate = entity.getRepairDate();

            ElecMaterial elecMaterial = elecMaterialMapper.selectElecMaterialByMaterialId(entity.getMaterialId());
            if (null != elecMaterial.getRepairCycle() && 0 != elecMaterial.getRepairCycle()) {
                int difference = HiultraUtil.differentDays(repairDate, nowDate);

                if (difference != 0) {
                    // 将变量转换类型
                    BigDecimal differenceTemp = new BigDecimal(difference);
                    BigDecimal san = new BigDecimal(30);
                    // 求出到目前为止已经过了几个月了
                    BigDecimal month = differenceTemp.divide(san, 2, BigDecimal.ROUND_HALF_UP);

                    BigDecimal repairperiodTemp = new BigDecimal(elecMaterial.getRepairCycle());
                    // 差值比维修周期大了 就是要维修了
                    if (month.compareTo(repairperiodTemp) == 1) {
                        if(null == warningId){
                            ElecWarning ew = new ElecWarning();
                            ew.setWarningType("1");
                            ew.setWarningTime(nowDate);
                            ew.setWarningContent("检修到期");
                            ew.setMaterialCategory("1");
                            elecWarningMapper.insertElecWarning(ew);
                            warningId = ew.getWarningId();

                            ElecWarningDetail ewd = new ElecWarningDetail();
                            ewd.setWarningId(warningId);
                            ewd.setMaterialId(entity.getMaterialId());
                            elecWarningDetailMapper.insertElecWarningDetail(ewd);
                        } else {
                            ElecWarningDetail ewd = new ElecWarningDetail();
                            ewd.setWarningId(warningId);
                            ewd.setMaterialId(entity.getMaterialId());
                            elecWarningDetailMapper.insertElecWarningDetail(ewd);
                        }
                    }
                }
            }

        }

        return 0;
    }
}
